#include "config.h"
#include <stdint.h>
#include <avr/eeprom.h>
#include "gui.h"

#define STARTADDR 00
#define SLOTINTERVAL 12
#define NUMBER_SLOTS 7

//helper routines for use of (u)int16 and (u)int8
#define make16(hi,lo) ((unsigned int) hi << 8) | (unsigned char) lo
#define make8(var,offset)	(var >> (offset * 8)) & 0xFF

/* SCREEN SPECIFIC CONSTANTS */
	//DOSING PARAMS SCREEN
	#define MAX_DOSE 60					//Standart max dose => 60ml


/* Enum for determining the value to load from / save to disk */
enum PARAM {
    TIME_INTERVAL_H,
	TIME_INTERVAL_MIN,
	FILL_LEVEL,
    DOSE,
	LAST_INJ_Y,
	LAST_INJ_M,
	LAST_INJ_D,
	LAST_INJ_H,
	LAST_INJ_MIN,
    SLOT_ACTIVE,
	FINE_TUNING_Y
};


/* SlotValue Struct. This struct is used during runtime to
   maintain the slot settings and sthe slot status.
*/
typedef struct slotValues_s{
	uint8  time_interval_h,
		   time_interval_min,
		   fill_level,
	       dose,
	       slot_active,
		   last_inj_y,
		   last_inj_m,
		   last_inj_d,
	       last_inj_h,
		   last_inj_min;
	uint16 fine_tuning_y;
} SlotValues;

SlotValues allSlotValues[7];


/* Error-Logger
   The Error-Log is a queue implemented with
   an array of the beneath struct and a position pointer.
   if the array is full, it starts overwriting old errors.
*/
typedef struct error_s{
	uint8 min,
		  h;
	unsigned char message[STRING_MAXLENGTH-7];
} ErrorLogType;

//error-log screen can show maximal MAX_LINES errors.
//To remember more errors makes no sense
ErrorLogType ErrorLog[MAX_LINES];
int8 lastErrorPointer;

//log level
enum LOG_LEVEL{
	NOTIFICATION = 0,
	ERROR = 1
};


/* Loads a stored slot status or setting from EEPROM and puts it in valueP.
	parameters:
		uint8 slotNumber:		The respective slot number
		enum PARAM valueType:	See above
		uint8* vauleP:			Pointer to the variable to save to
*/
void loadValue (uint8 slotNumber, enum PARAM valueType, uint8* valueP);


/* Saves a slot status or setting to EEPROM. The value is specified in iValue (uint8).
	parameters:
		uint8 slotNumber:		The respective slot number
		enum PARAM valueType:	See above
		uint8 iValue:			Pointer to the variable to save to
*/
void storeValue(uint8 slotNumber, enum PARAM valueType, uint8 iValue);


/* Saves a slot status or setting to EEPROM. The value is specified in iValue (int16).
   Currently only used to store the offset which is measured during calibration.
	parameters:
		uint8 slotNumber:		The respective slot number
		enum PARAM valueType:	See above
		uint8 iValue:			Pointer to the variable to save to
*/
void storeValue16(uint8 slotNumber, enum PARAM valueType, int16 iValue);


/* Saves dummy values for each setting of each slot to EEPROM. */
void bootstrap();


/* Checks for every slot if it is time for injection.
   If so, it triggers the injection at the slot and continues
   with checking after the dosing has taken place.
   	returns:		(uint8) If >= 1 slot had to be injected, 1
							is returned, otherwise 0
*/
uint8 checkIfInjectionNeeded();


/* Initiates the allSlotValues-array with the values loaded
   from EEPROM.
*/
void initSlotValueStruct();


/* calculates the time left until the next injection is needed
   for every slot and saves it in a 2d-array of int16.
   Array structure:
   				timeLeft[slotNumber][0] contains hours left
				timeLeft[slotNumber][1] contains minutes left
   The values could be negative.
   	parameters:
		int16* timeLeft:		int16 Array-Pointer with dimensions [7][2]
*/
void getTimeLeft(int16* timeLeft);


/* Logs error messages by writing it to the screen and save it to
   the error-log-queue. It logs also the time of occurrence.
*/
void logError(enum LOG_LEVEL level, unsigned char* message);


/* Loads all values of all slots at once from the eeprom. */
void loadAllValues();


/* stores all values of all slots at once to the eeprom */
void saveAllValues();
